package com.fll.fangyishare.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fll.fangyicommon.entity.ResultOk;
import com.fll.fangyicommon.entity.share.Liked;
import com.fll.fangyicommon.entity.share.Share;
import com.fll.fangyicommon.entity.user.User;
import com.fll.fangyishare.service.LikedService;
import com.fll.fangyishare.service.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author fll
 * create 2023/3/25
 */
@RestController
@Slf4j
@RequestMapping("/share")
public class LikedController {

    @Autowired
    private LikedService likedService;

    @Autowired
    private ShareService shareService;

    //登录用户点赞某个share
    @GetMapping("/addLiked")
    public ResultOk addLiked(@RequestHeader String userInfo, @RequestParam("shareId") Integer shareId) {
        ResultOk<Liked> ok = new ResultOk<>();
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }

        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            Boolean islLike = likedService.isLike(user.getUserId(), shareId);
            if (islLike){
                ok.setCode(400);
                ok.setMsg("已经点过赞啦");
                return ok;
            }
            //没有点过赞则添加记录
            Liked liked = new Liked();
            liked.setUserId(user.getUserId());
            liked.setShareId(shareId);
            Integer num = likedService.addLiked(liked);
            if (num != -1){
                ok.setCode(200);
                ok.setMsg("点赞成功");
                ok.setData(liked);
                return ok;
            }
            ok.setCode(400);
            ok.setMsg("点赞失败");
            return ok;
        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(500);
            ok.setMsg("后端异常");
            return ok;
        }
    }

    //登录用户取消某个share点赞
    @GetMapping("/cancelLiked")
    public ResultOk cancelLiked(@RequestHeader String userInfo, @RequestParam("shareId") Integer shareId) {
        ResultOk ok = new ResultOk<>();
        if (shareId == null){
            ok.setCode(400);
            ok.setMsg("shareId不能为空");
            return ok;
        }
        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            Liked liked = new Liked();
            liked.setUserId(user.getUserId());
            liked.setShareId(shareId);
            Integer num = likedService.cancelLiked(liked);
            if(num != -1){
                ok.setMsg("取消成功");
                ok.setCode(200);
            }else {
                ok.setCode(400);
                ok.setMsg("取消失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("后端异常");
        }
        return ok;
    }

    //获取登录用户所有点赞的share
    @GetMapping("/getLikeByUser")
    public ResultOk getLikeByUser(@RequestHeader String userInfo) {
        ResultOk<List<Share>> ok = new ResultOk<>();
        User user = null;
        try {
            user = new ObjectMapper().readValue(userInfo, User.class);
            //所有点赞的shareId     eg：(1,2,3....)
            String shareIds = likedService.getLikeByUser(user.getUserId());
            if (!shareIds.equals(")")){
                List<Share> likedShareList = shareService.getLikedShareByUser(user.getUserId(), shareIds);
                ok.setCode(200);
                ok.setMsg("查询成功");
                ok.setData(likedShareList);
            }else {
                ok.setCode(400);
                ok.setMsg("暂无点赞的share");
            }
        } catch (IOException e) {
            e.printStackTrace();
            ok.setCode(503);
            ok.setMsg("查询失败");
            return ok;
        }
        return ok;
    }




}
